"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from csr_utils import log

from commons import system_constatnts
from dao import job_dao, project_dao
from orm_domain.job import Job
from service.execution_service import ExecutionService
from utils.bean_utils import BeanUtils


class JobService:
    """
    任务管理，和execution 是一对多的关系，
    """

    def save(self, job: Job):
        if BeanUtils.str_is_blank(job.name):
            log.warning("job名称不能为空")
            return False
        if project_dao.get(id=job.project_id,is_del = system_constatnts.NOT_DEL_FLAG) is None:
            log.warning("job绑定的project不存在")
            return False
        job.name = job.name.strip()
        job_dao.save(job)
        return True

    def update(self, _id, name):
        job = job_dao.get_by_id(_id)
        if job:
            try:
                job.name = name
                job_dao.update()
                return True
            except Exception as e:
                log.error("更新job信息失败，错误为：", e)
                return False
        else:
            log.warning("更新job信息失败，job不存在！")
            return False

    def delete(self, _id):
        job = job_dao.get_by_id(_id)
        if job:
            try:
                job_dao.delete(job)
                return True
            except Exception as e:
                log.error("删除job信息失败，错误为：", e)
                return False
        else:
            log.warning("更新job信息失败，job不存在！")
            return False

    def get_by_id(self, job_id):
        job = job_dao.get_by_id(job_id)
        return job

    def get_list(self):
        job_list = job_dao.get_list()
        return job_list

    def bind_testcase_to_suite(self, job_id, suites: []):
        """
        测试用例绑定suite
        """
        try:
            job = job_dao.get_by_id(job_id)
            if job is None:
                log.warning("job不存在")
                return None
            job.suites = suites
            job_dao.update()
        except Exception as e:
            log.error("请检查测试套件列表数据是否正常", e)

    def unbind_testcase_to_suite(self, job_id):
        """
        测试用例解除绑定suite
        """
        try:
            job = job_dao.get_by_id(job_id)
            if job is None:
                log.warning("job不存在")
                return None
            job.suites = []
            job_dao.update()
        except Exception as e:
            log.error("请检查测试套件列表数据是否正常", e)

    def run(self, job_id, runner_id=None, remark=None):
        """
        传入执行的job
        """
        # todo: 返回成功或失败。
        try:
            job = job_dao.get_by_id(job_id)
            if job is None:
                log.warning("job不存在")
                return None
            job.execution_count += 1
            job_dao.update()
            exe = ExecutionService()
            exe.save(job, runner_id, remark)
            return True
        except Exception as e:
            log.error("执行job发生异常，请检查...", e)
            return False
